var calDesigner, calEntryWin;

function calendarEntryOpenFn(el, id) {
    calEntryWin = new dlr.cmp.workSpace.addEntryWindow();
    var feelId = calEntryWin.items.get(0).items.get(0).items.items[5];
    calEntryWin.items.get(0).items.get(0).items.items.remove(feelId);
    
    calEntryWin.addEntry(el, id);
    calEntryWin.show();
}; // end calendarPanelOpenFn

function calendarEntryEditFn(el, id, uid) {    
    calEntryWin = new dlr.cmp.workSpace.addEntryWindow();    
    calEntryWin.editEntry(el, id, uid);
    
    calEntryWin.show();
}; // end calendarPanelOpenFn

Ext.namespace("dlr.cmp.workSpace");
dlr.cmp.workSpace.calendarWindow = function(cfg) {
    // ------------------------------------------------
    // private properties
    // ------------------------------------------------
    var that = this;
    var currentDate = new Date();
    var windowCfg = {
        layout: "border",
        width: '100%',
        title: "Complete your day with Smiley",
        height: Ext.getBody().getHeight(),
        closeAction: "hide",
        closable: false,        
        items: [{
            region: 'center',
            layout: 'fit',
            id: 'calContainer',
            border: false,
            bodyBorder: false
}],
            buttons: [{
                text: " Previous ",
                handler: (function() {
                    currentDate.setMonth(currentDate.getMonth() - 1);
                    var calWinItem = Ext.getCmp('calContainer');
                    calWinItem.items.clear();
                    calWinItem.doLayout();
                    calWinItem.add({ layout: 'fit', border: false, autoScroll: true, html: getBody(that.users) }, true);
                    calWinItem.doLayout();
                })
            }, {
                text: " This ",
                handler: (function() {
                    currentDate = new Date();
                    var calWinItem = Ext.getCmp('calContainer');
                    calWinItem.items.clear();
                    calWinItem.doLayout();
                    calWinItem.add({ layout: 'fit', border: false, autoScroll: true, html: getBody(that.users) }, true);
                    calWinItem.doLayout();

                })
            }, {
                text: " Next ",
                handler: (function() {
                    currentDate.setMonth(currentDate.getMonth() + 1);
                    var calWinItem = Ext.getCmp('calContainer');
                    calWinItem.items.clear();
                    calWinItem.doLayout();
                    calWinItem.add({ layout: 'fit', border: false, autoScroll: true, html: getBody(that.users) }, true);
                    calWinItem.doLayout();

                })
            }, {
                text: "Close",
                handler: (function() { that.hide(); })
}],
                listeners: {
                    "show": function(cmp) {
                        if (!cmp.hasLoaded) {
                            cmp.hasLoaded = true;

                            //Ext.getCmp('calContainer').doLayout();
                            var calWinItem = Ext.getCmp('calContainer');
                            calWinItem.el.mask('Please wait!', 'x-mask-loading');
                            Ext.Ajax.request({
                                url: '../User/GetUsers',
                                params: { 'includeFeelings': true },
                                callback: function(options, success, response) {
                                    if (success) {
                                        var responseMessage = Ext.util.JSON.decode(response.responseText);
                                        var json = responseMessage.users;
                                        calWinItem.add({ layout: 'fit', border: false, autoScroll: true, html: getBody(json, new Date()) }, true);
                                        calWinItem.doLayout();
                                        calWinItem.el.unmask();
                                        cmp.users = json;
                                    } else {
                                        calWinItem.el.unmask();
                                        Ext.MessageBox.hide();
                                        Ext.MessageBox.show({
                                            title: "Loading failure",
                                            msg: response.responseText
                                        });
                                    }
                                    if (this.loadMask && this.container.ownerCt)
                                        this.container.ownerCt.el.unmask();
                                },
                                scope: this
                            });
                        }
                    }
} // end listeners
                };
                var ldMsk = null;
                // ------------------------------------------------
                // private functions
                // ------------------------------------------------

                function getBody(users) {
                    var dt = currentDate;
                    var daysgen = dt.getDaysInMonth();
                    // compensate for leap year
                    if (dt.getMonth() == 1) { // February only!
                        if ((dt.getFullYear() % 4 == 0 && dt.getFullYear() % 100 != 0) || dt.getFullYear() % 400 == 0) {
                            daysgen = 29;
                        }
                    }
                    var curMonth = dt.getMonth() + 1;
                    if (curMonth < 10) curMonth = '0' + curMonth;


                    var mybody = '<table width=100% style="background:#D5E0F4" cellspacing="1" cellpadding="2">';
                    //mybody += '<tr height="15" style="background:white">';
                    //mybody += '<td>Previous</td><td></td>';
                    //mybody += '</tr>';
                    mybody += '<tr height="30" style="background:white">';
                    for (j = 0; j <= daysgen; j++) {
                        if (j < 10) j = '0' + j;
                        if (j == 0) mybody += '<td><b>&nbsp; Name</b></td>';
                        else
                            mybody += '<td style="text-align:center"><font color="#696969" ><b>' + curMonth + '/' + j + '</b></font></td>';
                    }
                    mybody += '</tr>';
                    for (i = 0; i < users.length; i++) {
                        var user = users[i];
                        var feeling;
                        var onClk = "calendarEntryOpenFn(this,'" + user.UserID + "')";
                        mybody += '<tr height="40" style="background:white">';
                        for (j = 0; j <= daysgen; j++) {
                            var data = "";
                            if (j == 0) mybody += '<td><b>&nbsp;' + user.Name + '</b></td>';
                            else {
                                onClk = "calendarEntryOpenFn(this,'" + user.UserID + "')";
                                var fdt = leadingZero(dt.getMonth() + 1) + '/' + leadingZero(j) + '/' + dt.getFullYear(); //formatDate(user
                                for (k = 0; k < user.feelings.length; k++) {
                                    feeling = user.feelings[k];
                                    if (feeling && formatDate(feeling.Date, 'd') == fdt) {
                                        data = "<img title='" + feeling.Comment + "' src='Content/icons/smiley/" + trimAll(feeling.Feeling.toLowerCase()) + "32.gif'>";
                                        onClk = "calendarEntryEditFn(this,'" + feeling.FeelingsID + "','" + feeling.UserID + "')";
                                    }
                                }
                                if (feeling) {
                                    mybody += '<td feeling=\'' + Ext.util.JSON.encode(feeling) + '\' title="' + leadingZero(dt.getMonth() + 1) + '/' + leadingZero(j) + '/' + dt.getFullYear() + '" ondblclick="' + onClk + '">' + data + '</td>';
                                } else
                                    mybody += '<td title="' + leadingZero(dt.getMonth() + 1) + '/' + leadingZero(j) + '/' + dt.getFullYear() + '" ondblclick="' + onClk + '">' + data + '</td>';
                                feeling = undefined;
                            }
                        }
                        mybody += '</tr>';
                    }
                    mybody += '</table>';

                    return mybody;
                };

                function getCmps() {
                    return {
                        frmAccount: that.items.get(0).items.get(0).items.get(0),
                        frmUserName: that.items.get(0).items.get(0).items.get(0).items.get(0),
                        frmop: that.items.get(0).items.get(0).items.get(0).items.get(1),
                        frmnp: that.items.get(0).items.get(0).items.get(0).items.get(2),
                        frmcp: that.items.get(0).items.get(0).items.get(0).items.get(3),
                        frmFirstName: that.items.get(0).items.get(0).items.get(0).items.get(4),
                        frmLastName: that.items.get(0).items.get(0).items.get(0).items.get(5),
                        frmEmail: that.items.get(0).items.get(0).items.get(0).items.get(6)
                    };
                }; // end getCmps
                function basicVal(val) {
                    var myRegxp = /^([a-zA-Z0-9_-]+)$/;
                    var isVal = true;
                    if (myRegxp.test(val) == false) {
                        isVal = "Only Letters and Numbers are Allowed.";
                    }
                    if (val.length < 3) {
                        isVal = "Must be More Than 3 Characters.";
                    }
                    return isVal;
                }; // end basicVal
                // ------------------------------------------------
                // public properties
                // ------------------------------------------------
                // ------------------------------------------------
                // public methods
                // ------------------------------------------------
                // ------------------------------------------------
                // constructor code
                // ------------------------------------------------
                Ext.apply(this, cfg);
                // apply private cfgs
                Ext.apply(this, windowCfg);
                dlr.cmp.workSpace.calendarWindow.superclass.constructor.call(this, cfg);
                // events
            };
Ext.extend(dlr.cmp.workSpace.calendarWindow, Ext.Window);
Ext.reg("calendarWindow", dlr.cmp.workSpace.calendarWindow);